package cn.jeeweb.web.modules.sys.service.impl;

import cn.jeeweb.common.mybatis.mvc.service.impl.CommonServiceImpl;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.utils.TimeUtils;
import cn.jeeweb.web.modules.sys.entity.CodeTable;
import cn.jeeweb.web.modules.sys.entity.SysConfig;
import cn.jeeweb.web.modules.sys.mapper.CodeTableMapper;
import cn.jeeweb.web.modules.sys.service.ICodeTableService;
import cn.jeeweb.web.modules.sys.service.ISysConfigService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: code_table 服务实现
 */
@Transactional
@Service("codeTableService")
public class CodeTableServiceImpl extends CommonServiceImpl<CodeTableMapper, CodeTable> implements ICodeTableService {

    @Autowired
    ISysConfigService sysConfigService;

    @Override
    public String createNewCode(CodeTable codeTable) {

        String codeRule = "";
        EntityWrapper<SysConfig> entityWrapper = new EntityWrapper<>(SysConfig.class);
        entityWrapper.eq("code",codeTable.getCode());
        SysConfig sysConfig = sysConfigService.selectOne(entityWrapper);
        if(sysConfig != null){
            codeRule = sysConfig.getValue();
        }

        JSONObject jsonObject = JSON.parseObject(codeRule);
        String prefix = "";
        String dataFormat = "";
        String linkStr = "";
        int flowStart = 0;
        int flowStep = 0;
        int flowLen = 0;
        if(jsonObject != null){
            prefix = jsonObject.getString("prefix");
            dataFormat = jsonObject.getString("dataFormat");
            linkStr = jsonObject.getString("linkStr");
            flowStart = jsonObject.getInteger("flowStart");
            flowStep = jsonObject.getInteger("flowStep");
            flowLen = jsonObject.getInteger("flowLen");
        }

        String code = "";
        String format = TimeUtils.getCurrentTime(dataFormat);
        codeTable.setDateVal(format);
        CodeTable result = getCodeTable(codeTable);
        String numberStr = "";
        if (result != null) {
            String flowVal = result.getFlowVal();
            Integer number = Integer.parseInt(flowVal) + flowStep;
            numberStr = number.toString();
            StringBuffer stringBuffer = new StringBuffer();
            int valCount = numberStr.length();
            if (valCount < flowLen) {
                for (int i = 0; i < flowLen - valCount; i++) {
                    stringBuffer.append("0");
                }
            }
            numberStr = stringBuffer.toString() + numberStr;
            result.setFlowVal(numberStr);
            insertOrUpdate(result);
        }else{
            CodeTable newCodeTable = new CodeTable();
            newCodeTable.setCode(codeTable.getCode());
            newCodeTable.setDateVal(format);
            Integer number = flowStart + flowStep;
            numberStr = number.toString();
            StringBuffer stringBuffer = new StringBuffer();
            int valCount = numberStr.length();
            if (valCount < flowLen) {
                for (int i = 0; i < flowLen - valCount; i++) {
                    stringBuffer.append("0");
                }
            }
            numberStr = stringBuffer.toString() + numberStr;
            newCodeTable.setFlowVal(numberStr);
            insert(newCodeTable);
        }
        code = prefix + linkStr + format+ numberStr;
        return code;
    }

    @Override
    public CodeTable getCodeTable(CodeTable codeTable) {
        return baseMapper.getCodeTable(codeTable);
    }
}